package tk.mybatis.springboot.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.springboot.mapper.CommodityMapper;
import tk.mybatis.springboot.mapper.FollowMapper;
import tk.mybatis.springboot.mapper.IntegralMapper;
import tk.mybatis.springboot.mapper.LeavingmessageMapper;
import tk.mybatis.springboot.mapper.OrderMapper;
import tk.mybatis.springboot.mapper.UserMapper;
import tk.mybatis.springboot.mapper.UserMessageMapper;
import tk.mybatis.springboot.model.Commodity;
import tk.mybatis.springboot.model.CountermanInfo;
import tk.mybatis.springboot.model.Follow;
import tk.mybatis.springboot.model.TOrderExtend;
import tk.mybatis.springboot.model.TUser;
import tk.mybatis.springboot.model.TrankList;
import tk.mybatis.springboot.model.Usermessage;
import tk.mybatis.springboot.model.UserMessageExtend;
import tk.mybatis.springboot.service.JiYouService;
import tk.mybatis.springboot.share.util.BaiDuMapUtil;
import tk.mybatis.springboot.share.util.StringUtils;
import tk.mybatis.springboot.share.util.UniqId;
import tk.mybatis.springboot.wexin.bean.Userinfo;
@Service
@Transactional
public class JiYouServiceImpl implements JiYouService{
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired 
	private FollowMapper followMapper;
	
	@Autowired
	private UserMessageMapper userMessageMapper;
	
	@Autowired
	private CommodityMapper commodityMapper;
	
	@Autowired
	private IntegralMapper integralMapper;
	
	@Autowired
	private LeavingmessageMapper leavingmessageMapper;
	@Override
	public TUser getUserInfo(Userinfo userinfo) {
		TUser user = userMapper.getUserInfoByOpenId(userinfo.getOpenid());
		if(user == null){
			user = new TUser();
			user.setCreatedate(new Date());
			user.setModifydate(new Date());
			user.setMtype("user");
			user.setState("0");
			user.setId(UniqId.getUid());
			user.setOpenid(userinfo.getOpenid());
			user.setPic(userinfo.getHeadimgurl());
			user.setNickname(userinfo.getNickname());
			user.setScore(100);
			user.setFollowCount(0);
			user.setOrderCount(0);
			user.setSex("1".equals(userinfo.getSex())?1:0);
			userMapper.insertSelective(user);
		}else{
			user.setFollowCount(userMapper.getFollows(user.getId()).size());
			user.setOrderCount(orderMapper.getOrders(user.getId()).size());
		}
		return user;
	}
	@Override
	public List<TOrderExtend> getOrders(Userinfo userinfo) {
		TUser user = userMapper.getUserInfoByOpenId(userinfo.getOpenid());
		return orderMapper.getOrders(user.getId());
	}
	@Override
	public List<TUser> getFollows(Userinfo userinfo) {
		TUser user = userMapper.getUserInfoByOpenId(userinfo.getOpenid());
		return userMapper.getFollows(user.getId());
	}
	@Override
	public Boolean follows(Userinfo userinfo, String userId) {
		TUser user = userMapper.getUserInfoByOpenId(userinfo.getOpenid());
		Follow follow = followMapper.getFollowByUserIdTow(user.getId(), userId);
		if(follow != null){
			return false;
		}
		follow = new Follow();
		follow.setId(UniqId.getUid());
		follow.setCreatedate(new Date());
		follow.setUserid(user.getId());
		follow.setCountermanid(userId);
		int result = followMapper.insertSelective(follow);
		if(result == 1){
			userMapper.addUserFollows(userId);
			return true;
		}
		return false;
	}
	@Override
	public Boolean unFollows(Userinfo userinfo, String userId) {
		TUser user = userMapper.getUserInfoByOpenId(userinfo.getOpenid());
		Follow follow = followMapper.getFollowByUserIdTow(user.getId(), userId);
		int result = followMapper.deleteByPrimaryKey(follow.getId());
		if(result == 1){
			userMapper.minusUserFollows(userId);
			return true;
		}
		return false;
	}
	@Override
	public List<UserMessageExtend> getMessage(Userinfo userinfo, String type) {
		if("system".equals(type)){
			return userMessageMapper.getSystemMessage();
		}else{
		    TUser user = userMapper.getUserInfoByOpenId("1");
			List<UserMessageExtend> userMessage = userMessageMapper.getUserMessage(user.getId());
			return userMessage;
		}	
	}
	@Override
	public List<UserMessageExtend> getMessageInfo(Userinfo userinfo, String countermanId) {
		TUser user = userMapper.getUserInfoByOpenId(userinfo.getOpenid());
		return  userMessageMapper.getUserMessageInfo(user.getId(), countermanId);
	}
	@Override
	public Boolean setUserAddress(Userinfo userinfo, String latitude, String longitude) {
		TUser user = userMapper.getUserInfoByOpenId(userinfo.getOpenid());
		return 1 == userMapper.setUserAddress(user.getId(), latitude, longitude);
	}
	@Override
	public List<TUser> getCounterman(Userinfo userinfo) {
		List<TUser> countermans = userMapper.getCounterman();
		TUser user = userMapper.getUserInfoByOpenId(userinfo.getOpenid());
		String latitude = user.getLatitude();
		String longitude = user.getLongitude();
		if(!StringUtils.isEmpty(latitude) && !StringUtils.isEmpty(longitude)){
			for (TUser tUser : countermans) {
				Integer transport_mileage = BaiDuMapUtil.getAddByGeocoder_lc(latitude, longitude, tUser.getLatitude(), tUser.getLongitude()) / 1000;
				tUser.setTransport_mileage(transport_mileage);
			}
		}
		Collections.sort(countermans);
		return countermans;
	}
	@Override
	public CountermanInfo getCountermanInfo(String countermanId) {
		CountermanInfo countermanInfo = userMapper.findCountermanInfo(countermanId);
		countermanInfo.setCommoditys(commodityMapper.findCommodityAll());
		return countermanInfo;
	}
	@Override
	public TrankList getrankingList(String type) {
		TrankList trankList = new TrankList();
		if("month".equals(type)){
			trankList.setIntegrals(integralMapper.findIntegralMonth());
		}else if("year".equals(type)){
			trankList.setIntegrals(integralMapper.findIntegralYear());
		}
		trankList.setLeavingmessage(leavingmessageMapper.findLeavingmessageAll());
		return trankList;
	}
	@Override
	public Commodity getCommodity(String commodityId) {
		return commodityMapper.selectByPrimaryKey(commodityId);
	}
	@Override
	public Usermessage getSystemMessageInfo(String messageId) {
		return userMessageMapper.selectByPrimaryKey(messageId);
	}
	
}
